package cn.tansanqinger.dao.sql.comment;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.ICommenttopicSqlDao;
import cn.tansanqinger.pojo.Commenttopic;

@Repository
public class CommentTopicSqlDaoImpl {
	
	@Autowired
	private ICommenttopicSqlDao dao;
	
	/**
	 * 查询用户发表的评论
	 * @param uid
	 * @param page
	 * @return
	 */
	public List<Commenttopic> selectUserAll(int uid, int page){
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getUid, uid);
		Page<Commenttopic> pg = new Page<Commenttopic>(page,10);
		IPage<Commenttopic> iPage = dao.selectPage(pg, queryWrapper);
		return iPage.getRecords();
	}
	
	/**
	 * 用户评论的条数
	 * @param uid
	 * @return
	 */
	public int selectUserAllNumber(int uid) {
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getUid, uid);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 返回一定数量的被评论数据
	 * @param uid
	 * @param page
	 * @return
	 */
	public List<Commenttopic> replyToComment(int uid, int page){
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getWuid, uid);
		Page<Commenttopic> pg = new Page<Commenttopic>(page,10);
		IPage<Commenttopic> iPage = dao.selectPage(pg, queryWrapper);
		List<Commenttopic> commenttopics = iPage.getRecords();
		for (int i = 0; i < commenttopics.size(); i++) {//将查看的数据数据标记未查看过
			Commenttopic commenttopic = commenttopics.get(i);
			if(commenttopic.getIsselect()==0) {
				commenttopic.setIsselect(1);
				modification(commenttopic);
			}
		}
		return commenttopics;
	}
	
	/**
	 * 用户被回复的总信息数量
	 * @param uid
	 * @return
	 */
	public int replyToCommentCountAll(int uid) {
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getWuid, uid);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 查询被回复的数据总未被查看的信息
	 * @param uid
	 * @return
	 */
	public int replyToCommentCountIsSelect(int uid) {
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getWuid, uid).eq(Commenttopic::getIsselect, 0);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 查询数据，第一层
	 * @param tid
	 * @param page
	 * @return
	 */
	public List<Commenttopic> selectCommentTopicOneTiter(int tid, int page){
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getTid, tid).eq(Commenttopic::getCtid, 0).eq(Commenttopic::getCtiter, 1);
		Page<Commenttopic> pg = new Page<Commenttopic>(page, 10);
		IPage<Commenttopic> iPage = dao.selectPage(pg, queryWrapper);
		return iPage.getRecords();
	}
	
	/**
	 * 第一层数量
	 * @param tid
	 * @return
	 */
	public int selectCountOneTiter(int tid) {
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getTid, tid).eq(Commenttopic::getCtid, 0);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 查询数据，第二层
	 * @param tid
	 * @param ctid
	 * @param page
	 * @return
	 */
	public List<Commenttopic> selectCommentTopicTwoTiter(int tid, int ctid, int page){
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getTid, tid).eq(Commenttopic::getCtid, ctid).eq(Commenttopic::getCtiter, 2);
		Page<Commenttopic> pg = new Page<Commenttopic>(page, 10);
		IPage<Commenttopic> iPage = dao.selectPage(pg, queryWrapper);
		return iPage.getRecords();
	}
	
	/**
	 * 查询数据
	 * @param tid
	 * @param ctid
	 * @return
	 */
	public int selectCountTwoTiter(int tid, int ctid) {
		QueryWrapper<Commenttopic> queryWrapper = new QueryWrapper<Commenttopic>();
		queryWrapper.lambda().eq(Commenttopic::getTid, tid).eq(Commenttopic::getCtid, ctid).eq(Commenttopic::getCtiter, 2);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 通过主键
	 * @param cid
	 * @return
	 */
	public Commenttopic selectCidOne(int cid) {
		return dao.selectById(cid);
	}
	
	/**
	 * 添加信息
	 * @param commenttopic
	 * @return
	 */
	public int add(Commenttopic commenttopic) {
		dao.insert(commenttopic);
		return commenttopic.getCid();
	}
	
	/**
	 * 修改信息
	 * @param commenttopic
	 * @return
	 */
	public int modification(Commenttopic commenttopic) {
		return dao.updateById(commenttopic);
	}
	
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		return dao.deleteById(id);
	}

}
